const API = require('/wxapi/main.js')
App({

  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  navigateToLogin: false,
  onLaunch: function () {
    const that = this;
    // 检测新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function(){
      wx.showToast({
        title: '新版本升级中',
        icon: 'loading'
      })
      updateManager.applyUpdate()
    })
    /**
     * 初次加载判断网络情况
     */
    wx.getNetworkType({
      success: (res) => {
        const networkType = res.networkType
        if(networkType === 'none'){
          that.globalData.isConnected = false
          wx.showToast({
            title: '当前无网络',
            icon:'loading',
            duration: 2000
          })
        }
      },
    });
    /**
     * 监听网络状态变化
     */
    wx.onNetworkStatusChange((result) => {
      if(!res.isConnected){
        that.globalData.isConnected = false
        wx.showToast({
          title: '网络已断开',
          icon: 'loading',
          duration: 2000
        })
      }else{
        that.globalData.isConnected = true
        wx.hideToast()
      }
    })
    // 获取系统参数设置
    API.queryConfigBatch('mallName').then(res => {
      if(res.code == 0){
        res.data.forEach(ele => {
          wx.setStorageSync(ele.key, ele.value)
        })
      }
    })
  },
  goLoginPageTimeOut: function(){
    if(this.navigateToLogin){
      return
    }
    wx.removeStorageSync('token')
    this.navigateToLogin = true
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/authorize/index',
      })
    }, 1000)
  },
  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (e) {
    this.globalData.launchOption = e
    // 保存邀请人
    if(e && e.query && e.query.inviter_id){
      wx.setStorageSync('referrer', e.query.inviter_id)
    }
  },
  globalData: {
    isConnected: true,
    launchOption: undefined
  }
})
